package bar.dark.lee.setting.mybankauto;

import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.cashdrawing.InsertMyBankActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.DBSQLite.ParseSelectData;

public class MyBankAutoSelectActivity extends SuperActivity
{
	private DBSQLite sql;
	
	/** 이체 이름  */
	private TextView name;
	/** 출금 통장 뷰  */
	private View outBankView;
	/** 출금 통장 */
	private TextView outBank;
	/** 출금 카드 뷰  */
	private View outCardView;
	/** 출금 카드 */
	private TextView outCrad;
	/** 출금 카테고리  */
	private TextView outCategory;
	/** 입금 통장 */
	private TextView inBank;
	/** 입금 카테고리  */
	private TextView inCategory;
	/** 이체 금액 */
	private TextView money;
	/** 이체 일  */
	private TextView date;
	/** 수수료  */
	private TextView susuMoney;
	/** 내용  */
	private TextView memo;
	
	private Button deleteButton;
	
	private Button updateButton;
	
	private MyBankAutoData data;
	private MyBankAutoData newData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		sql = DBSQLite.getInstance(this);
		
		setContentView(R.layout.mybank_auto_select_activity);
		
		Intent intent = getIntent();
		
		data = intent.getParcelableExtra(MyBankAutoActivity.AUTO_DATA);
		
		setTitle("내통장 이체 정보");
		
		setFindView();
		setOnClickListener();
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		new ShowDialog().execute(BDUtil.asyncFlag, 1);
	}
	
	private void setFindView()
	{
		name = (TextView) findViewById(R.id.autoMyBankSelectName);
		outBankView = findViewById(R.id.autoMyBankSelectBankView);
		outBank = (TextView) findViewById(R.id.autoMyBankSelectOutBank);
		outCardView = findViewById(R.id.autoMyBankSelectCreditView);
		outCrad = (TextView) findViewById(R.id.autoMyBankSelectOutCredit);
		outCategory = (TextView) findViewById(R.id.autoMyBankSelectOutCategory);
		inBank = (TextView) findViewById(R.id.autoMyBankSelectInBank);
		inCategory = (TextView) findViewById(R.id.autoMyBankSelectInCategory);
		money = (TextView) findViewById(R.id.autoMyBankSelectMoney);
		date = (TextView) findViewById(R.id.autoMyBankSelectDate);
		susuMoney = (TextView) findViewById(R.id.autoMyBankSelectSuSuMoney);
		memo = (TextView) findViewById(R.id.autoMyBankSelectMemo);
		
		deleteButton = (Button) findViewById(R.id.autoMyBankDeleteButton);
		updateButton = (Button) findViewById(R.id.autoMyBankUpdateButton);
	}
	
	private void setOnClickListener()
	{
		deleteButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				new ShowDialog().execute(BDUtil.asyncFlag, 2);
			}
		});
		
		updateButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(MyBankAutoSelectActivity.this, InsertMyBankActivity.class);
				intent.putExtra(MyBankAutoActivity.AUTO_UPDATE_INSERT_FLAG, MyBankAutoActivity.AUTO_UPDATE);
				intent.putExtra(MyBankAutoActivity.AUTO_DATA, newData);
				startActivity(intent);
			}
		});
	}
	
	private void setData()
	{
		sql.selectMapTile(this, "SELECT * FROM auto_mybank WHERE id = " + data.getId() + ";", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				newData = null;
				
				int idIdx = c.getColumnIndex("id");
				int nameIdx = c.getColumnIndex("name");
				int outCategoryIdx = c.getColumnIndex("outcategory");
				int inCategoryIdx = c.getColumnIndex("incategory");
				int moneyIdx = c.getColumnIndex("money");
				int susuMoneyIdx = c.getColumnIndex("susumoney");
				int autodateIdx = c.getColumnIndex("autodate");
				int signdateIdx = c.getColumnIndex("signdate");
				int outkindIdx = c.getColumnIndex("outkind");
				int inkindIdx = c.getColumnIndex("inkind");
				int memoIdx = c.getColumnIndex("memo");
				int kindIdx = c.getColumnIndex("kind");
				
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					newData = new MyBankAutoData();
					
					newData.setId(c.getInt(idIdx) + "");
					newData.setName(c.getString(nameIdx));
					newData.setOutCategory(c.getString(outCategoryIdx));
					newData.setInCategory(c.getString(inCategoryIdx));
					newData.setMoney(c.getString(moneyIdx));
					newData.setSusuMoney(c.getString(susuMoneyIdx));
					newData.setAutodate(c.getInt(autodateIdx) + "");
					newData.setSigndate(c.getInt(signdateIdx) + "");
					newData.setOutkind(c.getString(outkindIdx));
					newData.setInkind(c.getString(inkindIdx));
					newData.setMemo(c.getString(memoIdx));
					newData.setKind(c.getInt(kindIdx) + "");
					c.moveToNext();
				}
			}
		});
	}
	
	private void setDeleteData()
	{
		String [] whereArgs = new String[]{data.getId()};
		
		sql.delete(this, "auto_mybank", "id = ?", whereArgs);
	}

	@Override
	protected void tipButton()
	{
	}

	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			if (BDUtil.btnEnable)
			{
				BDUtil.btnEnable = false;
				BDUtil.asyncFlag = -1;
				
				dialog = new ProgressDialog(MyBankAutoSelectActivity.this);
				dialog.setCancelable(false);
				dialog.setMessage("Loading...");
				dialog.show();
			}
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			
			if (arg0[0] == 0) {
				if (arg0[1] == 1)
				{
					setData();
					
					return arg0[1];
				}
				else if (arg0[1] == 2)
				{
					try
					{
						sql.beginTansaction(superContext);
						
						setDeleteData();
						
						sql.setTransactionSuccessful();
					} catch (Exception e)
					{
						return -1;
					} finally {
						try 
						{
							sql.endTransaction(superContext);
						} catch (Exception e) 
						{
							return -1;
						}
					}
					
					return arg0[1];
				}
				return arg0[0];
			} else 
				return arg0[0];
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			if (dialog != null) {
				dialog.dismiss();
			}
			
			if (result == -1)
			{
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
				
				BDUtil.showDialog(superContext, "오류", "데이터 저장에 실패했습니다.");
			}
			else if (result == 1)
			{
				if (newData != null)
				{
					name.setText(newData.getName());
					
					if (newData.getKind().equals("1"))
					{
						outBankView.setVisibility(View.VISIBLE);
						outCardView.setVisibility(View.GONE);
						outBank.setText(newData.getOutkind());
					}
					else if (newData.getKind().equals("2"))
					{
						outCardView.setVisibility(View.VISIBLE);
						outBankView.setVisibility(View.GONE);
						outCrad.setText(newData.getOutkind());
					}
					
					outCategory.setText(newData.getOutCategory());
					inBank.setText(newData.getInkind());
					inCategory.setText(newData.getInCategory());
					money.setText(BDUtil.getWon(newData.getMoney()) + shape);
					date.setText("매월 " + newData.getAutodate() + " 일");
					susuMoney.setText(BDUtil.getWon(newData.getSusuMoney()) + shape);
					memo.setText(newData.getMemo());
				}
				
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
			}
			else if (result == 2)
			{
				BDUtil.showDialogFinish(MyBankAutoSelectActivity.this, "알림", 
						"자동이체 데이터 " + data.getName() + "가 삭제 되었습니다.");
			}
		}
	}
}
